// 注：这个文件被用于管理项目中所有与“角色（职位）管理模块”有关的接口
import request from '@/utils/request'
import type {
    typeOfResponseDataOfTheApiThatGetPositionList,
    typeOfObjectThatRepresentsAPosition,
    typeOfResponseDataOfTheApiThatGetAllPermissionOfMenuAndButton,
} from '@/api/acl/role/type'
// 枚举地址↓
enum API {
    // 获取职位列表的接口地址↓
    getPositionList_URL = '/admin/acl/role/',
    // 添加职位的接口地址↓
    addAPosition_URL = '/admin/acl/role/save',
    // 更新职位的接口地址↓
    updateAPosition_URL = '/admin/acl/role/update',
    // 获取与某个职位对应的所有菜单和按钮权限的接口地址↓
    getAllPermissionOfMenuAndButton_URL = '/admin/acl/permission/toAssign/',
    // 分配权限给某个职位时需要用到的接口地址↓
    assignPermissionsForAPosition_URL = '/admin/acl/permission/doAssign/?',
    // 删除职位的接口地址↓
    deleteAPosition_URL = '/admin/acl/role/remove/',
}
/**
 * @description 获取职位列表的接口方法
 * @returns Promise<any> 返回一个promise对象，这个对象被解析成功后，会返回一个包含职位信息的数据对象
 */
export const requestGetPositionList = async (pageNumber: number, pageSize: number, positionName: string) => {
    try {
        let response = await request.get<any, typeOfResponseDataOfTheApiThatGetPositionList>(API.getPositionList_URL + `${pageNumber}/${pageSize}?roleName=${positionName}`)
        console.log(response) // {code: 200, message: '成功', data: {…}, ok: true}
        return response
    } catch (error: any) {
        console.error("获取职位列表失败", error)
    }
}
/**
 * @description 添加或更新职位的接口方法
 */
export const requestAddOrUpdateAPosition = async (positionInformation: typeOfObjectThatRepresentsAPosition) => {
    try {
        if (positionInformation.id) {
            let response = await request.put<any, any>(API.updateAPosition_URL, positionInformation)
            console.log(response)
            return response
        } else {
            let response = await request.post<any, any>(API.addAPosition_URL, positionInformation)
            console.log(response) // {code: 200, message: '成功', data: null, ok: true}
            return response
        }
    } catch (error: any) {
        console.error("添加或更新职位失败", error)
    }
}
/**
 * @description 函数“requestGetAllPermissionOfMenuAndButton”是获取与某个职位对应的所有菜单和按钮权限的接口方法。
 * @param positionId 职位的id号
 * @returns 
 */
export const requestGetAllPermissionOfMenuAndButton = async(positionId:number) => {
    try {
        let response = await request.get<any, typeOfResponseDataOfTheApiThatGetAllPermissionOfMenuAndButton>(API.getAllPermissionOfMenuAndButton_URL + positionId)
        console.log(response)
        return response
    } catch (error: any) {
        console.error("获取职位对应的所有菜单和按钮权限失败", error)
    }
}
// 分配权限给某个职位的接口方法（注：参数的类型是query）↓
export const requestAssignPermissionsForAPosition = async(positionId:number,permissionIds:number[]) => {
    try {
        let response = await request.post<any, any>(API.assignPermissionsForAPosition_URL + `roleId=${positionId}&permissionId=${permissionIds}`)
        return response
    } catch (error: any) {
        console.error("给某个职位分配权限失败", error)
    }
}
/**
 * @description “requestDeleteAPosition”是删除职位的接口方法
 * @param positionId 职位的id号
 * @returns 
 */
export const requestDeleteAPosition = async(positionId:number) => {
    try {
        let response = await request.delete<any, any>(API.deleteAPosition_URL + positionId)
        return response
    } catch (error: any) {
        console.error("删除职位失败", error)
    }
}